/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.vcs.advanced;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.text.*;
import org.openide.explorer.propertysheet.*;
import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor;
import org.openide.util.*;
import org.netbeans.modules.vcs.util.*;
import org.netbeans.modules.vcs.*;
/** User variables panel.
*
* @author Michal Fadljevic
*/
//-------------------------------------------
public class UserVariablesPanel extends JPanel implements EnhancedCustomPropertyEditor{
private Debug E=new Debug("UserVariablesPanel", true); // NOI18N
//private Debug D=E;
private JList list=null;
private DefaultListModel listModel=null;
private JButton editButton=null;
private JButton addButton=null;
private JButton removeButton=null;
private UserVariablesEditor editor;
//-------------------------------------------
static final long serialVersionUID =-4165869264994159492L;
public UserVariablesPanel(UserVariablesEditor editor){
this.editor = editor;
initComponents();
initListeners();
deselectAll();
//setPreferredSize(screenSize);
}
//-------------------------------------------
private JButton createButton(String name){
JButton button = new JButton(name);
return button;
}
//-------------------------------------------
private JScrollPane createList(){
list=new JList();
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listModel=new DefaultListModel();
list.setModel(listModel);
Vector variables=(Vector)editor.getValue();
int len=variables.size();
for(int i=0;i<len;i++){
listModel.addElement(variables.elementAt(i).toString ());
}
JScrollPane listScrollPane = new JScrollPane(list);
return listScrollPane;
}
//-------------------------------------------
private JPanel createCommands(){
editButton=createButton(g("CTL_Edit")); // NOI18N
editButton.setMnemonic(KeyEvent.VK_E);
addButton=createButton(g("CTL_Add")); // NOI18N
addButton.setMnemonic(KeyEvent.VK_A);
removeButton=createButton(g("CTL_Remove")); // NOI18N
removeButton.setMnemonic(KeyEvent.VK_R);
GridLayout panel2Layout=new GridLayout(5,1);
panel2Layout.setVgap(5);
JPanel panel2=new JPanel();
panel2.setLayout(panel2Layout);
panel2.setBorder(new EmptyBorder(5, 7, 5, 7));
panel2.add(addButton);
panel2.add(editButton);
panel2.add(removeButton);
JPanel panel=new JPanel(new BorderLayout());
panel.add(panel2,BorderLayout.NORTH);
return panel;
}
//-------------------------------------------
public void initComponents(){
GridBagLayout gb=new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gb);
setBorder(new TitledBorder("Variables"));
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
JScrollPane listScrollPane=createList();
gb.setConstraints(listScrollPane,c);
add(listScrollPane);
c = new GridBagConstraints();
JPanel commandPanel=createCommands();
//c.fill = GridBagConstraints.BOTH;
//c.weightx = 0.1;
//c.weighty = 1.0;
gb.setConstraints(commandPanel,c);
add(commandPanel);
Dimension preferred = listScrollPane.getPreferredSize();
preferred.setSize((int) (Toolkit.getDefaultToolkit().getScreenSize().width*0.8), preferred.height);
listScrollPane.setPreferredSize(preferred);
}
//-------------------------------------------
private void initListeners(){
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
//D.deb("valueChanged "+e); // NOI18N
updateButtons();
}
});
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
if( e.getClickCount()==2 ){
editVariable();
}
updateButtons();
}
});
list.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
//D.deb("keyPressed() e="+e); // NOI18N
int keyCode=e.getKeyCode();
switch( keyCode ){
case KeyEvent.VK_INSERT:
addVariable();
//TODO better insertVariable(int index)
break;
case KeyEvent.VK_DELETE:
removeVariable();
break;
case KeyEvent.VK_ENTER:
editVariable();
break;
default:
//D.deb("ignored keyCode="+keyCode); // NOI18N
}
updateButtons();
}
});
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
editVariable();
}
});
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
addVariable();
}
});
removeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
removeVariable();
}
});
}
//-------------------------------------------
private void deselectAll(){
list.clearSelection();
removeButton.setEnabled(false);
editButton.setEnabled(false);
}
//-------------------------------------------
private void updateButtons(){
if( list.getSelectedIndex()<0 ){
deselectAll();
}
else{
removeButton.setEnabled(true);
editButton.setEnabled(true);
list.requestFocus();
}
}
//-------------------------------------------
private void editVariable(){
//D.deb("editVariable()"); // NOI18N
int index=list.getSelectedIndex();
if( index<0 ){
return ;
}
VcsConfigVariable var = (VcsConfigVariable) ((Vector) editor.getValue ()).get (index);
EditUserVariable ev=new EditUserVariable(new Frame(), var);
ev.setLocationRelativeTo(list);
ev.show();
if( ev.wasCancelled()==false ){
listModel.setElementAt(var.toString (), index);
}
list.requestFocus();
updateButtons();
editor.setValue( getPropertyValue() );
}
//-------------------------------------------
private void addVariable(){
VcsConfigVariable var = new VcsConfigVariable ("", "", "", false, false, false, ""); // NOI18N
EditUserVariable ev=new EditUserVariable(new Frame(), var);
ev.setLocationRelativeTo(list);
ev.show();
if( ev.wasCancelled()==false ){
((Vector) editor.getValue ()).add (var);
listModel.addElement(var.toString ());
}
list.requestFocus();
updateButtons();
editor.setValue( getPropertyValue() );
}
//-------------------------------------------
private void removeVariable(){
int index=list.getSelectedIndex();
if( index<0 ){
return ;
}
((Vector) editor.getValue ()).remove (index);
listModel.removeElementAt(index);
updateButtons();
editor.setValue( getPropertyValue() );
}
//-------------------------------------------
public Object getPropertyValue() {
//D.deb("getPropertyValue()");
return editor.getValue ();
}
//-------------------------------------------
String g(String s) {
return NbBundle.getBundle
("org.netbeans.modules.vcs.cmdline.Bundle").getString (s);
}
String g(String s, Object obj) {
return MessageFormat.format (g(s), new Object[] { obj });
}
String g(String s, Object obj1, Object obj2) {
return MessageFormat.format (g(s), new Object[] { obj1, obj2 });
}
String g(String s, Object obj1, Object obj2, Object obj3) {
return MessageFormat.format (g(s), new Object[] { obj1, obj2, obj3 });
}
//-------------------------------------------
}
/*
* <<Log>>
* 4 Gandalf-post-FCS1.2.2.0 3/23/00 Martin Entlicher Fix of long panel width.
* 3 Gandalf 1.2 1/27/00 Martin Entlicher NOI18N
* 2 Gandalf 1.1 11/27/99 Patrik Knakal
* 1 Gandalf 1.0 11/24/99 Martin Entlicher
* $
*/